package com.mars.admin.modules.sys.service.impl;

import com.mars.admin.modules.sys.entity.SensorInfoEntity;
import com.mars.admin.modules.sys.entity.SensorNeedfixEntity;
import com.mars.admin.modules.sys.service.SensorInfoService;
import com.mars.admin.modules.sys.service.SensorNeedfixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mars.admin.common.utils.PageUtils;
import com.mars.admin.common.utils.Query;

import com.mars.admin.modules.sys.dao.SensorBattrystatusDao;
import com.mars.admin.modules.sys.entity.SensorBattrystatusEntity;
import com.mars.admin.modules.sys.service.SensorBattrystatusService;


@Service("sensorBattrystatusService")
public class SensorBattrystatusServiceImpl extends ServiceImpl<SensorBattrystatusDao, SensorBattrystatusEntity> implements SensorBattrystatusService {

    @Autowired
    private SensorInfoService sensorInfoService;

    @Autowired
    private SensorNeedfixService sensorNeedfixService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SensorBattrystatusEntity> page = this.selectPage(
                new Query<SensorBattrystatusEntity>(params).getPage(),
                new EntityWrapper<SensorBattrystatusEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void generatefix(Date gatherdate) {

        List<SensorBattrystatusEntity> sensorBattrystatusEntities =  baseMapper.queryListbyDateOrderByCharge(gatherdate);
        int i = 0;
        for (SensorBattrystatusEntity sensorBattrystatusEntity:sensorBattrystatusEntities) {
            SensorNeedfixEntity sensorNeedfixEntity = new SensorNeedfixEntity();
            sensorNeedfixEntity.setSensorId(sensorBattrystatusEntity.getSensorId());
            sensorNeedfixEntity.setBattstatusId(sensorBattrystatusEntity.getId());
            sensorNeedfixEntity.setBatteryStatus(sensorBattrystatusEntity.getBatteryStatus());
            sensorNeedfixEntity.setGatherTime(sensorBattrystatusEntity.getGatherTime());
            sensorNeedfixEntity.setChangeStatus("0");
            if (i<20) {
                sensorNeedfixEntity.setChangeUser(1);
                sensorNeedfixEntity.setUserName("张三");
            } else if (i>=20 && i<40) {
                sensorNeedfixEntity.setChangeUser(2);
                sensorNeedfixEntity.setUserName("李四");
            } else {
                sensorNeedfixEntity.setChangeUser(3);
                sensorNeedfixEntity.setUserName("王五");
            }

            sensorNeedfixService.insert(sensorNeedfixEntity);
            i++;
        }

    }

    @Override
    public void generatecharge(Date gatherdate) {
        //
        List<SensorBattrystatusEntity> sensorBattrystatusEntities =  baseMapper.queryListbyDate(gatherdate);
        if (sensorBattrystatusEntities.size() > 0) {return;}

        Random random = new Random();

        List<SensorInfoEntity> sensorInfoEntities = sensorInfoService.selectList(new EntityWrapper<SensorInfoEntity>());
        for (SensorInfoEntity sensorInfoEntity:sensorInfoEntities) {
            SensorBattrystatusEntity sensorBattrystatusEntity = new SensorBattrystatusEntity();
            sensorBattrystatusEntity.setSensorId(sensorInfoEntity.getId());
            sensorBattrystatusEntity.setBatteryStatus(new BigDecimal(random.nextDouble()));
            sensorBattrystatusEntity.setGatherTime(gatherdate);
            baseMapper.insert(sensorBattrystatusEntity);
        }


    }
}
